package com.example.zg.androidnewview.behaviors;

import android.content.Context;
import android.graphics.Canvas;
import android.support.annotation.NonNull;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * 创建日期:2019/1/10 on 15:42
 * 描述：
 * 作者: zhougan
 */
public class Behavior3 extends CoordinatorLayout.Behavior<View> {
    private static final String TAG = "Behavior3";
    public Behavior3(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {


        return dependency instanceof AppBarLayout;
    }
    private float disY=0;//AppBarLayout 上移到消失的距离
    private float startY=0;

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
        Log.i(TAG, "onDependentViewChanged: ");
       //当AppBarLayout底部和标题底部重合时,完全显示标题
        // 计算出AppBarLayout底部到标题底部的距离,
        if(disY==0){
            disY=dependency.getY()+dependency.getHeight()-(child.getY()+child.getHeight());
            startY= dependency.getY();
        }
        float y=startY-dependency.getY();//AppBarLayout移动的距离
        float alpha=y/disY;
        if(alpha>1){
            alpha=1;
        }else if(alpha<0){
            alpha=0;
        }

        child.setAlpha(alpha);
        return true;
    }
}
